//
//  SliderController.m
//  BAlliance
//
//  Created by coding Tools on 16/7/13.
//  Copyright © 2016年 jack. All rights reserved.
//

#import "SliderController.h"

@interface SliderController () <UICollectionViewDelegate, UICollectionViewDataSource>

/// 顶部选择视图
@property(nonatomic, strong) UIView *headSlideView;  //40 //1 //98.173.253
/// 选中 蓝色条部分
@property(nonatomic, strong) UIView *sliderView;
/// 按钮 记录选中状态
@property(nonatomic, strong) UIButton *selectedBtn;
/// 存储蓝色线宽的数组
@property(nonatomic, strong) NSArray *lineWidth;
/** ----  */
/// 滑动列表
@property(nonatomic, strong) UICollectionView *collectionView;

/// 子控制器
@property(nonatomic, strong) NSArray *controllers;
/// 子控制器 title
@property(nonatomic, strong) NSArray *titles;

/// 顶部视图的高度
@property(nonatomic, assign) CGFloat headHeight;

@end

@implementation SliderController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
}

- (void)prepareForSuperViewContronlls:(NSArray *)controllers titles:(NSArray *)titles headHeight:(CGFloat)headHeight
{
    self.controllers = controllers;
    self.titles = titles;
    self.headHeight = headHeight;
    [self.view addSubview:self.headSlideView];
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"seliderControllerCell"];
    for (UIViewController *controller in controllers) {
        [self addChildViewController:controller];
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.controllers.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"seliderControllerCell" forIndexPath:indexPath];
    UIViewController *viewController = self.controllers[indexPath.item];
    [viewController.view setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - self.headHeight)];
    [cell addSubview:viewController.view];
    return cell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;
    
    [self selectedHeadWithIndex:index];
}
- (void)selectedHeadWithIndex:(NSInteger)index
{
    for (UIView *view in self.headSlideView.subviews)
    {
        if ([view.class isSubclassOfClass:[UIButton class]])
        {
            UIButton *btn = (UIButton *)view;
            if (btn.tag == index)
            {
                btn.selected = YES;
            }
            else
            {
                btn.selected = NO;
            }
        }
    }
    [self changeSliderViewsFrameWithIndex:index];
}

- (UICollectionView *)collectionView
{
    if (_collectionView == nil)
    {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 64 - self.headHeight);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.headHeight, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - self.headHeight)collectionViewLayout:flowLayout];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.bounces = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
    }
    return _collectionView;
}

- (UIView *)headSlideView
{
    if (_headSlideView == nil)
    {
        CGFloat btnWidth = SCREEN_WIDTH / self.controllers.count;
        UIFont *font = [UIFont systemFontOfSize:14];
        _headSlideView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.headHeight)];
        UIColor *blue = RGB(0, 147, 255); // RGB(98, 173, 253)
        _headSlideView.backgroundColor = [UIColor whiteColor];
        NSMutableArray *lineWidth = [NSMutableArray array];
        for (NSInteger i = 0; i < self.controllers.count; i++)
        {
            UIButton *button = [[UIButton alloc] init];
            button.frame = CGRectMake(btnWidth * i, 0, btnWidth, self.headHeight - 2.5);
            button.tag = i;
            button.titleLabel.font = font;
            [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            [button setTitleColor:blue forState:UIControlStateSelected]; // RGB(0, 147, 255)
            
            if (i == 0) {
                button.selected = YES;
                self.selectedBtn = button;
            }
            
            NSString *text = self.titles[i];

            NSNumber *number = [[NSNumber alloc] initWithFloat:([text sizeWithAttributes:@{NSFontAttributeName: font}].width + 10)];
            [lineWidth addObject:number];
            
            [button setTitle:text forState:UIControlStateNormal];
            [_headSlideView addSubview:button];
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        self.lineWidth = [NSArray arrayWithArray:lineWidth.copy];
        self.sliderView = [[UIView alloc] initWithFrame:CGRectMake(0, self.headHeight - 2.5, SCREEN_WIDTH / self.titles.count, 2.5)];
        self.sliderView.backgroundColor = blue;
        [_headSlideView addSubview:self.sliderView];
        
        UIView *bottomLine = [[UIView alloc] init];
        bottomLine.frame = CGRectMake(0, self.headHeight - 0.5, SCREEN_WIDTH, 0.5);
        bottomLine.backgroundColor = RGB(238, 238, 238);
        [_headSlideView addSubview:bottomLine];
    }
    return _headSlideView;
}
- (void)buttonClick:(UIButton *)sender
{
    self.selectedBtn.selected = NO;
    sender.selected = YES;
    self.selectedBtn = sender;
    
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    [self changeSliderViewsFrameWithIndex:sender.tag];
}

- (void)changeSliderViewsFrameWithIndex:(NSInteger)index
{
    [UIView animateWithDuration:0.25 animations:^{
        self.sliderView.frame = CGRectMake((SCREEN_WIDTH / self.titles.count) * index, self.headHeight - 2.5, SCREEN_WIDTH / self.titles.count, 2);
    }];
}

- (void)gotoItem:(NSIndexPath *)indexPath
{
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    [self selectedHeadWithIndex:1];
}

@end
